package com.gsp.gspframeworktest.service.impl;

import com.gsp.gspframework.annotation.Service;
import com.gsp.gspframework.annotation.Transactional;
import com.gsp.gspframework.bean.FileParam;
import com.gsp.gspframework.helper.DatabaseHelper;
import com.gsp.gspframework.helper.UploadHelper;
import com.gsp.gspframeworktest.entity.Customer;
import com.gsp.gspframeworktest.service.CustomerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl implements CustomerService {

    private static final Logger log = LoggerFactory.getLogger(CustomerServiceImpl.class);

    @Override
    public List<Customer> getCustomerList() {
        String sql = "select * from customer";
        return DatabaseHelper.queryEntityList(Customer.class, sql);
    }

    @Override
    public List<Customer> getCustomerList(String keyword) {
        return DatabaseHelper.getEntityList(Customer.class);
    }

    @Override
    public Customer getCustomer(long id) {
        return DatabaseHelper.getEntity(Customer.class, id);
    }

    @Override
    public boolean createCustomer(Map<String, Object> param) {
        return DatabaseHelper.insertEntity(Customer.class, param);
    }

    @Override
    public boolean updateCustomer(long id, Map<String, Object> param) {
        return DatabaseHelper.updateEntity(Customer.class, id, param);
    }

    @Override
    public boolean deleteCustomer(long id) {
        return DatabaseHelper.deleteEntity(Customer.class, id);
    }

    @Transactional
    @Override
    public boolean createCustomer(Map<String, Object> fieldMap, FileParam fileParam) {
        boolean result = DatabaseHelper.insertEntity(Customer.class, fieldMap);
        if (result) {
            UploadHelper.uploadFile("D:/tmp/", fileParam);
        }
        return result;
    }
}
